{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from dataset.Dataset import TGS_Dataset\n",
    "from model.Models import UnetSeNext50\n",
    "from contextlib import contextmanager\n",
    "import time\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "@contextmanager\n",
    "def timer(title):\n",
    "    t0 = time.time()\n",
    "    yield\n",
    "    print(\"{} - done in {:.0f}s\".format(title, time.time() - t0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "TRAIN_PATH = './train/'\n",
    "# AUX_PATH = './Data/auxiliary_data'\n",
    "LOAD_PATHS = None\n",
    "# LOAD_PATHS = ['./Saves7/UnetSeNext50/2018-10-05 01:49_Fold1_Epoach54_reset0_val0.877',\n",
    "#              './Saves3/UnetSeNext50/2018-10-02 18:17_Fold2_Epoach36_reset0_val0.878',\n",
    "#              './Saves5/UnetSeNext50/2018-10-03 19:38_Fold3_Epoach49_reset0_val0.857',\n",
    "#              './Saves3/UnetSeNext50/2018-10-02 23:43_Fold4_Epoach46_reset0_val0.863',\n",
    "#              './Saves/UnetSeNext50/2018-10-02 00:22_Fold5_Epoach135_reset1_val0.872']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "##############################\n",
    "LOSS = 'lovasz'\n",
    "OPTIMIZER = 'SGD'\n",
    "PRETRAINED = True\n",
    "N_EPOCH = 120   ## 120 or 140 is better, 30 just for debug\n",
    "BATCH_SIZE = 32\n",
    "NET = UnetSeNext50\n",
    "###########OPTIMIZER###########\n",
    "LR = 1e-2\n",
    "T_MAX = 60    ## 60 or 70 is better, 15 just for debug\n",
    "T_MUL = 1\n",
    "LR_MIN = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1562 empty masks out of 4000 total masks\n"
     ]
    }
   ],
   "source": [
    "train_dataset = TGS_Dataset(TRAIN_PATH)\n",
    "loaders, ids = train_dataset.yield_dataloader(num_workers=11, batch_size=BATCH_SIZE, nfold=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i, (train_loader, val_loader) in enumerate(loaders, 1):\n",
    "    with timer('Fold {}'.format(i)):\n",
    "        net = NET(lr=LR, fold=i)\n",
    "        net.define_criterion(LOSS)\n",
    "        net.optmizer_and_scheduler(T_max=T_MAX, T_mul=T_MUL, lr_min=LR_MIN)\n",
    "    \n",
    "        if LOAD_PATHS is not None:\n",
    "            if LOAD_PATHS[i - 1] is not None:\n",
    "                net.load_model(LOAD_PATHS[i - 1])\n",
    "\n",
    "        net.train_and_valid(train_loader, val_loader, n_epoch=N_EPOCH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
